<template>
	<jk-dialog
		:dialog-state="showDialog"
		title="工序日产量明细"
		width="1000px"
		:show-confirm-btn="false"
		:show-cancel-btn="false"
		:show-close-btn="true"
		@on-close="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<div ref="box1" class="flex-between-center">
			<div class="flex-start-center-wrap">
			</div>
			<div class="flex-end-center-wrap">
				<jk-select v-model="searchParams.workshopId" :option-list="workshopList" clearable placeholder="区域" size="small" class="search-bar-item-size1 margin-5-0 item-gap" @change="plantChange">
				</jk-select>
				<jk-select v-model="searchParams.processCode" :clearable="false" value-key-name="code" placeholder="工序" :option-list="processList" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-select>
				<jk-select v-model="searchParams.shiftId" clearable placeholder="班次" :option-list="shiftList" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
				<div class="search-bar-item-size2 item-gap">
					<el-date-picker v-model="searchParams.belongDate" :clearable="false" :picker-options="pickerOptions" size="small" style="width: 100%;" type="date" value-format="yyyy-MM-dd" placeholder="日期" />
				</div>
				<jk-search-button @click="onSearchBtn"></jk-search-button>
			</div>
		</div>
		<jk-table
			:data="tableData"
			border
			:loading="tableLoading"
			:height="dialogTableHeight - 30"
			:footer-method="footerMethod"
			show-footer
		>
			<!--			<vxe-column-->
			<!--				v-for="(item, index) in tableColumnList"-->
			<!--				:key="index"-->
			<!--				:title="item.title"-->
			<!--				:align="item.align"-->
			<!--				:field="item.keyName"-->
			<!--				:sortable="item.sortable"-->
			<!--				:fixed="item.fixed ? item.fixed : ''"-->
			<!--				:min-width="item.minWidth"-->
			<!--				:width="item.width"-->
			<!--			>-->
			<!--				<template slot-scope="scope">-->
			<!--					<span>{{ scope.row[item.keyName] }}</span>-->
			<!--				</template>-->
			<!--			</vxe-column>-->
			<vxe-column field="machineName" title="设备"></vxe-column>
			<vxe-column field="productName" title="品种"></vxe-column>
			<vxe-column field="batchCode" title="批号"></vxe-column>
			<vxe-column field="shiftOutput" title="产量（米）"></vxe-column>
			<vxe-column field="shiftQty" title="产量（KG）"></vxe-column>
			<vxe-column v-if="searchParams.processCode === 'DB'" field="allNum" title="打包"></vxe-column>
			<vxe-column v-else field="shiftElectricEnergy" title="能耗"></vxe-column>
		</jk-table>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { productionPrdReportGetProcessDayData } from '../../../api/production/reports';
    import { pageConfig } from '../../../utils/jk/jk-tool';
    import { systemUserColumnConfigGet } from '../../../api/system/column-config';
    import XEUtils from 'xe-utils';
    import { workforcemanagementShiftPage } from '../../../api/schedule/shift';
    import { getInfo } from '@/api/login';
    export default {
        mixins: [calHeight],
        components: {},
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            params: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        data() {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                shiftList: [],
                workshopList: [],
                tableLoading: false,
                showDialog: false,
                processList: [],
                tableData: [],
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    workshopId: ''
                },
                tableColumnList: [],
                noCalKey: ['machineName', 'productName', 'batchCode']
            };
        },
        methods: {
            // 自定义方法来隐藏列
            hideColumn(field) {
                // 遍历columns，找到对应field的列并设置visible为false
                this.tableColumnList.forEach(column => {
                    console.log(column);
                    if (this.searchParams.processCode === 'DB' && column.keyName === 'shiftElectricEnergy') {
                        column.ifShow = false;
                    }
                });
            },
            footerMethod({ columns, data }) {
                return [
                    columns.map((column, columnIndex) => {
                        if (columnIndex === 0) {
                            return `合计`;
                        }
                        if (!this.noCalKey.includes(column.property)) {
                            return XEUtils.sum(data, column.property);
                        }
                        return null;
                    })
                ];
            },
            onSearchBtn() {
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
            },
            getMenuColumnListRequest() {
                return systemUserColumnConfigGet({ path: 'processYieldDailyReport', filterShow: true }).then(res => {
                    if (res.data.status === 200) {
                        this.tableColumnList = res.data.res.columnConfigList && res.data.res.columnConfigList.length ? res.data.res.columnConfigList[0].itemList : [];
                    }
                });
            },
            getListRequest() {
                this.tableLoading = true;
                return productionPrdReportGetProcessDayData(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            plantChange() {
                this.getShiftListRequest();
            },
            getShiftListRequest() {
                const params = JSON.parse(JSON.stringify(this.searchParams));
                params.auditState = 1;
                return workforcemanagementShiftPage(params).then(res => {
                    if (res.data.status === 200) {
                        this.shiftList = res.data.res || [];
                        this.$set(this.searchParams, 'shiftId', this.shiftList && this.shiftList.length ? this.shiftList[0].id : null);
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                if (!this.searchParams.workshopId) {
                    this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                }
                await this.getMenuColumnListRequest();
                await this.getShiftListRequest();
                await this.getListRequest();
                // this.hideColumn();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.$set(this.searchParams, 'processCode', this.params.processCode);
                    this.$set(this.searchParams, 'belongDate', this.params.belongDate);
                    this.processList = this.$store.state.user.userProcess.processList;
                    this.getDependentData();
                }
            }
        }
    };
</script>
